import {Api} from '@/api/type'
import ApiCommon from '@/api/common'
import {BaseApi} from '@/api/base-api'

type R<T> = Api.Common.R<T>;
type Data = Api.System.Role;
type RoleRelateRoute = Api.System.RoleRelateRoute;
type PageParameter<T = Record<string, any>> = Api.Common.PageParameter<T>;

export class RoleApi extends BaseApi<Data> {
    constructor() {
        super('role')
    }

    /**
     * 获取角色关联路由
     * @param identity 角色ID
     */
    async getRoleRelateRoute<T = R<Array<RoleRelateRoute>>>(identity: string): Promise<T> {
        return await ApiCommon.request.get<T>('system/role/get-role-route-relate', {identity: identity})
    }

    /**
     *
     * 添加角色关联路由
     * @param roleId 角色ID
     * @param routeId 路由ID列表
     */
    async addRoleRelateRoute<T = R<boolean>>(roleId: string, routeId: Array<string>): Promise<T> {
        return await ApiCommon.request.post<T>('system/role/add-role-route-relate', {
            roleId: roleId,
            routeId: routeId
        })
    }

    /**
     * 获取角色关联用户分页
     * @param parameter 角色ID
     */
    async getRoleRelateUser<T>(parameter: PageParameter<{identity: string}>): Promise<R<T>> {
        parameter.page = parameter.page || 1
        parameter.size = parameter.size || 10
        return await ApiCommon.request.get<R<T>>(`system/role/get-role-relate-user`, parameter)
    }

    /**
     * 添加角色关联用户
     * @param roleId 角色ID
     * @param userId 用户ID数组
     */
    async addRoleRelateUser<T = R<boolean>>(roleId: string, userId: Array<string>): Promise<T> {
        return await ApiCommon.request.post<T>('system/role/add-role-relate-user', {
            roleId: roleId,
            userId: userId
        })
    }

    /**
     * 移除角关联用户
     * @param roleId 角色ID
     * @param userId 用户ID数组
     */
    async removeRoleRelateUser<T = R<boolean>>(roleId: string, userId: Array<string>): Promise<T> {
        return await ApiCommon.request.post<T>('system/role/remove-role-relate-user', {
            roleId: roleId,
            userId: userId
        })
    }

}

